﻿using ConfExpert.Forms.Base;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ConfExpert.Utility
{
    public partial class DominantColors : XForm
    {
        public DominantColors()
        {
            InitializeComponent();
        }

        private void DominantColors_Load(object sender, EventArgs e)
        {
            
            bbiHesapla.ItemClick += bbi_ItemClick;
            bbiGetir.ItemClick += bbiGetir_ItemClick;

            d = new DataTable("clr");
            d.Columns.Add("ProductId", typeof(int));
            d.Columns.Add("clr1", typeof(int));
            d.Columns.Add("clr2", typeof(int));
            d.Columns.Add("clr3", typeof(int));
            d.Columns.Add("clr4", typeof(int));
            d.Columns.Add("d1", typeof(double));
            d.Columns.Add("d2", typeof(double));
            d.Columns.Add("d4", typeof(double));
            d.Columns.Add("dx", typeof(double));

        }

        void bbiGetir_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.Init("GetSelection");
            gridView1.AddSummary();
            gridView1.BestFitColumns();
        }

        DataTable d;
        Bitmap GetBitmap(byte[] x)
        {
            Bitmap bmp;
            using (var ms = new MemoryStream(x))
            {
                bmp = new Bitmap(ms);
                return bmp;
            }
        }

        void bbi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            int ProductId = 0;

            while (true)
            {
                DataTable dt = ExecuteDataTable("GetProductPhotos", ProductId);
                foreach (DataRow dr in dt.Rows)
                {
                    ProductId = (int)dr["ProductId"];
                    Color clr1 = GetDominantColor(GetBitmap((byte[])dr["Photo1"]));
                    Color clr2 = GetDominantColor(GetBitmap((byte[])dr["Photo2"]));
                    Color clr3 = GetDominantColor(GetBitmap((byte[])dr["Photo3"]));
                    Color clr4 = GetDominantColor(GetBitmap((byte[])dr["Photo4"]));

                    double d1 = ColourDistance(clr1, clr2);
                    double d2 = ColourDistance(clr1, clr4);
                    double d4 = ColourDistance(clr2, clr4);

                    d.Rows.Add(ProductId,
                        clr1.ToArgb(), clr2.ToArgb(), clr3.ToArgb(), clr4.ToArgb(),
                        d1, d2, d4, d1 + d2 + d4);

                    if (ProductId % 100 == 0)
                    {
                        GC.Collect();
                        Notify(ProductId.ToString());
                    }
                }

                if (dt.Rows.Count == 0)
                    break;
            }

            gridView1.Init(d);
            gridView1.AddSummary();
            gridView1.BestFitColumns();

        }


        public Color GetDominantColor(Bitmap image)
        {
            //http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale
            //TODO: Use LockBits to get an array of the Pixels.  Count the occurrences of each color.  Find the top five.

            if (image != null)
            {
                BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0,
                                         image.Width, image.Height),
                                         ImageLockMode.ReadOnly,
                                         PixelFormat.Format32bppArgb);

                int a = 0, r = 0, g = 0, b = 0;
                unsafe
                {
                    byte* imagePointer1 = (byte*)bitmapData1.Scan0;
                    for (int i = 0; i < bitmapData1.Height; i++)
                    {
                        for (int j = 0; j < bitmapData1.Width; j++)
                        {
                            a++;
                            b += imagePointer1[0];
                            g += imagePointer1[1];
                            r += imagePointer1[2];
                            imagePointer1 += 4;
                        }

                        //4 bytes per pixel
                        imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4);
                    }

                }

                image.UnlockBits(bitmapData1);

                return Color.FromArgb(r / a, g / a, b / a);
            }


            return Color.Transparent;
        }



        double ColourDistance(Color e1, Color e2)
        {
            long rmean = ((long)e1.R + (long)e2.R) / 2;
            long r = (long)e1.R - (long)e2.R;
            long g = (long)e1.G - (long)e2.G;
            long b = (long)e1.B - (long)e2.B;
            return Math.Sqrt(r * r + g * g + b * b);
            //return Math.Sqrt((((512 + rmean) * r * r) >> 8) + 4 * g * g + (((767 - rmean) * b * b) >> 8));
        }

    }
}
